package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.Contents;
import com.zwz.bean.Title;
import com.zwz.dao.ContentsDao;
import com.zwz.dao.TitleDao;
import com.zwz.service.TitleService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TitleServiceImpl implements TitleService {

    @Autowired
    private TitleDao titleDao;

    @Autowired
    private ContentsDao contentsDao;

    @Override
    public boolean insert(Title title) {
        return titleDao.insert(title) == 1 ;
    }

    @Override
    public boolean deleteById(Integer tid) {
        Map<String,Object> map = new HashMap<>();
        map.put("tid",tid);
        contentsDao.deleteByMap(map);
        return titleDao.deleteById(tid) == 1 ;
    }

    @Override
    public boolean updateById(Title title) {
        return titleDao.updateById(title) == 1;
    }

    @Override
    public Title getById(Integer tid) {
        return titleDao.selectById(tid);
    }

    @Override
    public IPage<Title> getPage(int currentPage, int pageSize, Title title) {
        IPage<Title> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Title> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(title.getTitle()), Title::getTitle, title.getTitle());
        lqw.orderByDesc(Title::getTid);
        titleDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int countAll() {
        return titleDao.countAll();
    }

    @Override
    public List<Title> queryList(Title title) {
        return null;
    }
}
